package HelperFinancial.DataBase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

public class ConfigurationAdapter {
	
	private ConfigurationHelper BDHelper;
	
	private static ConfigurationAdapter instance;

	private ConfigurationAdapter(Context context) {
		this.BDHelper = new ConfigurationHelper(context);
		
	}
	
	public void populate(){
		Date today = new Date();  
		  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(today);  
  
        calendar.set(Calendar.DAY_OF_MONTH, 1);
		
		
		
		SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
		String dateInit = formatador.format(calendar.getTime());
		calendar.add(Calendar.MONTH, 1);
		String dateEnd = formatador.format(calendar.getTime());
		
		String categoria = "Todos";
		
		List<String> config = getAllConfigurations();
		
		if(config.size() == 0){
			BDHelper.insert(dateInit.substring(3, dateInit.length()), dateEnd, categoria);
		}else{
			String id = getId();
			BDHelper.updateConfigurationCategory(categoria, id);
			BDHelper.updateConfigurationDateInit(dateInit.substring(3, dateInit.length()), id);
			BDHelper.updateConfigurationDateEnd(dateEnd, id);
		}
	}
	
	public static ConfigurationAdapter getInstance(Context context){
		if(instance == null){
			instance = new ConfigurationAdapter(context);
		}
		
		return instance;
	}
	
	public static void destroy() {
		if(instance != null){
			instance.close();
		}
		instance = null;
	}
	
	private void close() {
		BDHelper.close();
	}

	
	public List<String> getAllConfigurations() {
		Cursor cr = BDHelper.getAllConfiguration();
		List<String> allConfig = new ArrayList<String>();
		int colId = cr.getColumnIndex("_id");
		int colInit = cr.getColumnIndex("dateinit");
		int colEnd = cr.getColumnIndex("dateend");
		int colCategory = cr.getColumnIndex("category");
		
		
		
		while(cr.moveToNext()){
			String n = cr.getInt(colId) + "|" + cr.getString(colInit)+ "|" +cr.getString(colEnd) + "|" + cr.getString(colCategory);
			allConfig.add(n);
		}
		return allConfig;
	}

	
	public void updateCategory(String categoria, String id){
		BDHelper.updateConfigurationCategory(categoria, id);
	}
	
	public void updateDateInit(String dateInit, String id){
		BDHelper.updateConfigurationDateInit(dateInit, id);
	}
	
	public void updateDateEnd(String dateEnd, String id){
		BDHelper.updateConfigurationDateEnd(dateEnd, id);
	}
	
	public void removeAll(){
		BDHelper.removeAll();
	}
	
	public String getId(){
		return getAllConfigurations().get(0).split("\\|")[0];
	}
	
	public String getDateInit(){
		return getAllConfigurations().get(0).split("\\|")[1];
	}
	
	public String getDateEnd(){
		return getAllConfigurations().get(0).split("\\|")[2];
	}
	
	public String getCategory(){
		return getAllConfigurations().get(0).split("\\|")[3];
	}

}
